<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Array类型</title>
</head>
<body>
<script>
    var animals = new Array('cat', 'dog', 'caw', 'goal');
    var cars = ['Benz', 'BMW', 'Audi', 'Volkswagen', 'Honda'];

    animals.length = 2;
    console.log(animals[2]);  // undefined
    animals[animals.length] = 'pig'
    console.log(animals)  // ['cat', 'dog', 'pig']
    //cars[99] = 'Toyota'
    //console.log(cars.length)  // 100

    console.log(animals instanceof Array); //true
    console.log(Array.isArray(cars)); //true

    cars.push('Mazda');
    console.log(cars); // ['Benz', 'BMW', 'Audi', 'Volkswagen', 'Honda', 'Mazda']
    cars.unshift('Buick')
    console.log(cars)  // ['Buick', 'Benz', 'BMW', 'Audi', 'Volkswagen', 'Honda', 'Mazda']
    cars.pop();
    console.log(cars);  // ['Buick', 'Benz', 'BMW', 'Audi', 'Volkswagen', 'Honda']
    cars.shift();
    console.log(cars); // ['Benz', 'BMW', 'Audi', 'Volkswagen', 'Honda']

    cars.sort();
    console.log(cars);  // ["Audi", "BMW", "Benz", "Honda", "Volkswagen"]
    var nums = [3, 6, 10, 55];
    console.log(nums.sort())  // [10, 3, 55, 6]
    nums.sort(function(a, b){ return a - b });
    console.log(nums); // [3, 6, 10, 55]

    animals = animals.concat(['fox', 'bear'], 'monkey')
    console.log(animals); //  ["cat", "dog", "pig", "fox", "bear", "monkey"]
    var tmpAnimals = animals.slice(2)
    console.log(tmpAnimals); // ["pig", "fox", "bear", "monkey"]
    tmpAnimals = animals.slice(1, 3);
    console.log(tmpAnimals); // ["dog", "pig"]
    tmpAnimals = animals.splice(1, 1);
    console.log(tmpAnimals); // ['dog']
    console.log(animals);  // ["cat", "pig", "fox", "bear", "monkey"]
    tmpAnimals = animals.splice(2, 0, 'dolphin', 'shark')
    console.log(tmpAnimals); // []
    console.log(animals); // ["cat", "pig", "dolphin", "shark", "fox", "bear", "monkey"]

    console.log(cars.indexOf('Audi')) // 0
    cars.push('Audi');
    console.log(cars.lastIndexOf('Audi'))  // 5
    console.log(cars.indexOf('Audi', 3)); // 5
</script>
</body>
</html>